﻿using QD.DbManager;
using QD.LopPlat.UI.Code;
using QD.LopPlat.UI.Code.DbContent;
using QD.LopPlat.UI.Model.Sys;
using QD.SQLHelper;
using QD.SyncServers;
using QD.Tools;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace QD.LopPlat.UI
{
    public partial class DataBaseSel : BaseForm
    {
        public DataBaseSel()
        {
            InitializeComponent();

            DataGridViewHelper.InitGrid(this.grid, false);
        }

        private void DataBaseSel_Load(object sender, EventArgs e)
        {
            this.KeyDown += DataBaseSel_KeyDown;

            BindDb();

            this.textBox1.Focus();
            this.grid.ClearSelection();
        }

        void DataBaseSel_KeyDown(object sender, KeyEventArgs e)
        {
            if (GeneralTools.IsSystemKeysF8(e))
            {
                RSAEncryption rsa = new RSAEncryption();
                rsa.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindDb();
        }

        private void BindDb()
        {
            DbQfDevPlat dbPlat = new DbQfDevPlat();

            string key = this.textBox1.Text.Trim();
            IList<QDDataBase> dbList = dbPlat.QDDataBase.OrderBy(p => p.QfName).ToList();

            dbList = dbList.Where(p => (p.QfCode.Contains(key) || p.QfName.Contains(key) || p.MasterDataBase.Contains(key))).OrderBy(p => p.QfName).ToList();
            this.grid.DataSource = dbList;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindDb();
            }
        }

        private void grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                DataGridViewRow row = this.grid.Rows[e.RowIndex];

                string qfCode = row.Cells["QfCode"].Value.ToString();
                DataBaseMag mag = new DataBaseMag(qfCode);
                mag.Show();
            }
        }

        private void DataBaseSel_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void DataBaseSel_Activated(object sender, EventArgs e)
        {
            this.textBox1.Focus();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DialogResult dialog = Tools.PromptHelper.PromptOkCancel("确认注销当前用户？");
            if (dialog == DialogResult.OK)
            {
                QDUserHelper.ClearLoginStatus();
                Login login = new Login();
                login.Show();
                this.Hide();
            }
        }

        private void 升级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SyncStep1 frm = new SyncStep1();
            frm.Show();
        }

        private void 平台用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneralHelper.OpenPcForm("Sys_UidPwdMag");
        }

        private void 数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = Application.OpenForms["DatabasesMag"];
            if (form == null)
            {
                form = new DatabasesMag(DbConfig.Instance.LogonConfig);
                form.Show();
            }
            else
            {
                form.Activate();
            }
        }

        private void 表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = Application.OpenForms["TableManager"];
            if (form == null)
            {
                form = new TableManager(DbConfig.Instance.LogonConfig, DbConfig.Instance.DbMasterName);
                form.Show();
            }
            else
            {
                form.Activate();
            }
        }

        private void 视图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = Application.OpenForms["ViewManager"];
            if (form == null)
            {
                form = new ViewManager(DbConfig.Instance.LogonConfig, DbConfig.Instance.DbMasterName);
                form.Show();
            }
            else
            {
                form.Activate();
            }
        }

        private void qDSEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = Application.OpenForms["QDSEditor"];
            if (frm == null)
            {
                frm = new QDSEditor(DbConfig.Instance);
                frm.Show();
            }
            else
            {
                frm.Activate();
            }
        }

        private void 内容CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneralHelper.OpenPcForm("QDDevPlatHelp");
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneralHelper.OpenPcForm("AboutUs");
        }

        private void 生成RSAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Create_RSA form = new Create_RSA();
            form.ShowDialog();
        }

        private void 注册升级ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RSAEncryption rsa = new RSAEncryption();
            rsa.Show();
        }

        private void 应用用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (QDDataBaseHelper.CurQDDataBase == null)
            {
                MessageBox.Show("请选择一个应用！");
            }
            else
            {
                GeneralHelper.OpenPcForm("Sys_UidPwdQfMag");
            }
        }

        private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                IList<DataGridViewRow> rows = this.grid.GetSelectRows();
                DataGridViewRow row = rows[0];
                string qfcode = row.Cells["QfCode"].Value.ToString();


                DbQfDevPlat dbPlat = new DbQfDevPlat();
                QDDataBase curPlat = dbPlat.QDDataBase.FirstOrDefault(p => p.QfCode == qfcode);

                QDDataBaseHelper.CurQDDataBase = curPlat;
            }
        }

        private void 新增应用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataBaseMag mag = new DataBaseMag();
            mag.Show();
        }

        private void 重置管理员账号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认重置系统管理员admin账号？", "提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                try
                {

                    DbQfDevPlat dbPlat = new DbQfDevPlat();

                    string sql = @"if((select count(*) from [Sys_UidPwd] where uid='admin')=0)
begin
insert into [Sys_UidPwd](Uid,UserName,Utype,Pwd,ForbidLogon,CreateDate)
values ('admin','系统管理员',0,'0192023A7BBD73250516F069DF18B500',0,getdate())
end
else
begin
update [Sys_UidPwd] set Pwd='0192023A7BBD73250516F069DF18B500',UserName='管理员',Utype=0 where Uid='admin'
end";

                    dbPlat.Database.ExecuteSqlCommand(sql);

                    MessageBox.Show("重置成功！");

                }
                catch (Exception ex)
                {
                    Tools.PromptHelper.Prompt(ex);
                }
            }
        }
    }
}
